﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyClassLibrary;
using MyDB;

namespace MyProgram
{
    public partial class FrmStaffs : FrmBase
    {
        public FrmStaffs()
        {
            InitializeComponent();
        }

        MedicareDataClassesDataContext db = new MedicareDataClassesDataContext();

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "")
            {
                var q = from f in db.Staffs
                        join j in db.JobTitles on f.職稱ID equals j.職稱ID
                        join d in db.Divisions on f.科別ID equals d.科別ID
                        select new {f.醫療人員ID, f.姓名,d.科別名,j.職稱,f.帳號,f.密碼,f.電話,f.Email};

                this.dataGridView1.DataSource = q.ToList();
            }
            else
            {
                var q = from f in db.Staffs
                        join j in db.JobTitles on f.職稱ID equals j.職稱ID
                        join d in db.Divisions on f.科別ID equals d.科別ID
                        where f.姓名.Contains(this.textBox1.Text)
                        select new { f.醫療人員ID, f.姓名, d.科別名, j.職稱, f.帳號, f.密碼, f.電話, f.Email };         
            
                this.dataGridView1.DataSource = q.ToList();
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //開啟新增
            FrmNewStaff fs = new FrmNewStaff();
            fs.ShowDialog();

            var q = from f in db.Staffs
                    join j in db.JobTitles on f.職稱ID equals j.職稱ID
                    join d in db.Divisions on f.科別ID equals d.科別ID
                    select new { f.醫療人員ID, f.姓名, d.科別名, j.職稱, f.帳號, f.密碼, f.電話, f.Email };

            this.dataGridView1.DataSource = q.ToList();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.DataSource != null)
            {
                clsUser user = new clsUser();
                user.userID = int.Parse(this.dataGridView1.SelectedCells[0].Value.ToString());
                user = user.getUser();

                FrmPersonProfile fd = new FrmPersonProfile(user);

                fd.ShowDialog();

                MedicareDataClassesDataContext dba = new MedicareDataClassesDataContext();

                var q = from f in dba.Staffs
                        join j in dba.JobTitles on f.職稱ID equals j.職稱ID
                        join d in dba.Divisions on f.科別ID equals d.科別ID
                        select new { f.醫療人員ID, f.姓名, d.科別名, j.職稱, f.帳號, f.密碼, f.電話, f.Email };

                this.dataGridView1.DataSource = q.ToList();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

    }
}
